// 简单本地鉴权工具（localStorage）

const STORAGE_KEY = 'pp_auth_v1'

export function setAuth(auth) {
  const payload = {
    username: auth?.username || '',
    role: auth?.role || '', // 'student' | 'teacher'
    loginAt: Date.now(),
  }
  localStorage.setItem(STORAGE_KEY, JSON.stringify(payload))
}

export function getAuth() {
  try {
    const raw = localStorage.getItem(STORAGE_KEY)
    return raw ? JSON.parse(raw) : null
  } catch {
    return null
  }
}

export function isLoggedIn() {
  const a = getAuth()
  return !!(a && a.role)
}

export function logout() {
  localStorage.removeItem(STORAGE_KEY)
}

